package com.anlu.session.session;

import com.anlu.session.utils.CookieBasedSession;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CasFilter implements Filter {

    public static final String USER_INFO = "user";

    private RedisTemplate redisTemplate;

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        MyRequestWrapper myRequestWrapper = new MyRequestWrapper(request,redisTemplate);

        //如果是未登录状态，进入下面的逻辑
        String requestUrl=request.getServletPath();
        if (!"/toLogin".equals(requestUrl)
                && !requestUrl.startsWith("/login")
                && !myRequestWrapper.isLogin()) {
            String ticket = request.getParameter("ticket");
            if(null==ticket || null == redisTemplate.opsForValue().get(ticket)){
                HttpServletResponse response = (HttpServletResponse)servletResponse;
                response.sendRedirect("http://cas.com:8090/toLogin?url="+request.getRequestURL().toString());
                return ;
            }
            /**
             * 是自动登录请求，则种cookie值进去，-本次请求是302重定向
             * 重定向后的下次请求，自带cookie,则直接是登录状态
             */
            myRequestWrapper.setSessionId((String) redisTemplate.opsForValue().get(ticket));
            myRequestWrapper.createSession();

            //种cookie
            CookieBasedSession.onNewSession(myRequestWrapper,(HttpServletResponse)servletResponse);
            //重定向自转一次，原地跳转重定向一次
            HttpServletResponse response = (HttpServletResponse)servletResponse;
            response.sendRedirect(request.getRequestURL().toString());
            return;
        }
        try {
            filterChain.doFilter(myRequestWrapper,servletResponse);
        } finally {
            myRequestWrapper.commitSession();
        }
    }

    @Override
    public void destroy() {

    }
}
